#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int n;
typedef long long ll;
int sum[N];
void add(int x) {
  for (int i = x; i < N; i += (i & -i)) {
    sum[i] += 1;
  }
}
ll query(int x) {
  ll ans = 0;
  for (int i = x; i > 0; i -= (i & -i)) {
    ans += sum[i];
  }
  return ans;
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    memset(sum, 0, sizeof(sum));
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i], a[i] = n - a[i] + 1;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
      ans += query(a[i]);
      add(a[i]);
    }
    cout << ans << endl;
  }
}
